var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var cors = require('cors');

// 引入主页路由
var indexRouter = require('./routes/index');
// 引入品牌公寓路由
var apartRouter = require('./routes/bandApartment');
// 引入积分商城路由
var scoreRouter=require('./routes/score')
// 引入签到路由
var siginRouter=require('./routes/sigins')
// 引入消息路由
var messgeRouter=require('./routes/messge')

var usersRouter = require('./routes/users');
var detailRouter = require('./routes/detail');

var wishRouter = require("./routes/wish")

var roommateRouter = require("./routes/roommate")

// 登录注册
var userRouter = require("./routes/login");

var app = express();

var mongoose = require('mongoose');
mongoose.connect('mongodb://101.34.123.189:27017/balerabbit', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});
//判断是否数据库链接成功 成功即打印
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("数据库连接成功");
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(cors());


app.use('/api/home', indexRouter);
app.use('/api/band', apartRouter);
app.use('/api/score',scoreRouter);
app.use('/api/sigin',siginRouter);
app.use('/api/wish',wishRouter);
app.use('/api/score',scoreRouter)
app.use('/api/sigin',siginRouter)
app.use('/api/detail',detailRouter)
app.use('/api/message',messgeRouter)
app.use('/api/roommate',roommateRouter);
app.use('/api/user', userRouter);

app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
